<?php
//
// +---------------------------------------------------------------------------------+
// | Package: EezeeSite Core                                                         |
// +---------------------------------------------------------------------------------+
// | Copyright (C) 2006  EezeeSite Development Team                                  |
// +---------------------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or                   |
// | modify it under the terms of the GNU General Public License                     |
// | as published by the Free Software Foundation; either version 2                  |
// | of the License, or (at your option) any later version.                          |
// |                                                                                 |
// | This program is distributed in the hope that it will be useful,                 |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of                  |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                   |
// | GNU General Public License for more details.                                    |
// |                                                                                 |
// | You should have received a copy of the GNU General Public License               |
// | along with this program; if not, write to the Free Software                     |
// | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. |
// +---------------------------------------------------------------------------------+
// | Author: Mayukh De <mayukhde@gmail.com>                                          |
// +---------------------------------------------------------------------------------+
//

class ESUser
{
    var $userID;
    var $DB;
    
    function ESUser()
    {
        global $DB;
        $this->DB = $DB;
    }
    
    function getRegUserGroupID()
    {
        $groups = new ESDBOps($this->DB, ES_GROUPS_TBL);
        $groups->resetVars(ES_GROUPS_TBL);
        $groups->sql['regGroupID'] = array(
                                          'select' => 'group_id',
                                          'where'  => 'group_define_name=\''.$GLOBALS['ES']['CLIENTCONF']['member_group'].'\''
                                          );
        $regGroupID = $groups->select('regGroupID');
        return $regGroupID[0];                                           
    }
    
    function registerNewUser($data)
    {
        global $ESPerms;
        $regUserGroupID = $this->getRegUserGroupID();
        $regNewUser = array(
                           'perm_type'           => $regUserGroupID,
                           'auth_container_name' => 'DB'
                           );
        $newUserID = $ESPerms->addUser($regNewUser);
        $newUserToGroup = array(
                               'perm_user_id' => $newUserID,
                               'group_id'     => $regUserGroupID
                               );
        $ESPerms->addUserToGroup($newUserToGroup);
        $usersDB = new ESDBOps($this->DB, ES_USERS_TBL);
        $usersDB->resetVars(ES_USERS_TBL);
        $newUserVals = array(
                            'user_id'    => $newUserID,
                            'handle'     => strtolower($GLOBALS['ES']['CLIENTCONF']['admin_name']),
                            'email'      => $GLOBALS['ES']['CLIENTCONF']['admin_email'],
                            'password'   => md5($GLOBALS['ES']['CLIENTCONF']['admin_password']),
                            'lastaccess' => time(),
                            'lastcookie' => $_COOKIE[$GLOBALS['ES']['CLIENTCONF']['cookie_name'].'_SESSION'],
                            'lastip'     => $_SERVER['REMOTE_ADDR'],
                            'is_active'  => 1,
                            'user_group' => $regUserGroupID
                            );
        $usersDB->insert($newUserVals);
        $usersInfoDB = new ESDBOps($this->DB, ES_USERSXTRAINFO_TBL);
        $usersInfoDB->resetVars(ES_USERSXTRAINFO_TBL);
        $usersXtraVals = array(
                              'user_id'    => $newUserID,
                              'signupdate' => time(),
                              'updates'    => 0,
                              'lastupdate' => time(),
                              'searchable' => 1
                              );
        $usersInfoDB->insert($usersXtraVals);
        $personalArea = array(
                             'application_id'   => $applicationID,
                             'area_define_name' => $GLOBALS['ES']['PERM']['AREAS'][1].'_'.$newUserID
                             );
        $personalAreaID = $ESPerms->addArea($personalArea);
        for ($i = 0; $i < count($GLOBALS['ES']['PERM']['AREAPERMS']['USER']['LEVEL1']); $i++) {
            $rightOpts = array(
                              'area_id'           => $personalAreaID,
                              'right_define_name' => $GLOBALS['ES']['PERM']['AREAPERMS']['USER']['LEVEL1'][$i]
                              );
            $rightID[$i] = $ESPerms->addright($rightOpts);
        }
        $rightID = null;
        for ($i = 0; $i < count($GLOBALS['ES']['PERM']['AREAPERMS']['USER']['LEVEL2']); $i++) {
            $rightOpts = array(
                              'area_id'           => $personalAreaID,
                              'right_define_name' => $GLOBALS['ES']['PERM']['AREAPERMS']['USER']['LEVEL2'][$i]
                              );
            $rightID[$i] = $ESPerms->addright($rightOpts);
            $newRightOpt = array(
                                'group_id' => $regUserGroupID,
                                'right_id' => $rightID[$i]
                                );
            $ESPerms->grantGroupRight($newRightOpt);
        }
        $rightID = null;
        for ($i = 0; $i < count($GLOBALS['ES']['PERM']['AREAPERMS']['USER']['LEVEL3']); $i++) {
            $rightOpts = array(
                              'area_id'           => $personalAreaID,
                              'right_define_name' => $GLOBALS['ES']['PERM']['AREAPERMS']['USER']['LEVEL3'][$i]
                              );
            $rightID[$i] = $ESPerms->addright($rightOpts);
            $newRightOpt = array(
                                'perm_user_id' => 1,
                                'right_id'     => $rightID[$i]
                                );
            $ESPerms->grantUserRight($newRightOpt);
        }                                                        
        $this->postRegisterAction($newUserID);
    }

    function postRegisterAction($newUserID)
    {
    }
}

?>